#!/bin/bash
#
# Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Update configuration for systemd to enable locale time
enable_local_rtc() {
  msg "Enable local rtc"
  cat > /etc/adjtime << EOF

0
LOCAL
EOF
}

# Setup locale and timezone.
# This function used in hook_manager.sh and first_boot_setup.sh
setup_locale_timezone() {
  local DI_LOCALE DI_TIMEZONE LOCALE
  DI_LOCALE=$(installer_get "DI_LOCALE")
  DI_TIMEZONE=$(installer_get "DI_TIMEZONE")

  DI_LOCALE=${DI_LOCALE:-en_US}
  DI_TIMEZONE=${DI_TIMEZONE:-Etc/UTC}
  LOCALE=${DI_LOCALE%.*}

  # Disable all locales first.
  sed -i 's/^\([^#].*\)$/# \1/g' /etc/locale.gen

  # Always generate en_US locale.
  msg "Generating locale: en_US ${LOCALE}"
  sed -i "s/# \(en_US\.UTF-8.*$\)/\1/g" /etc/locale.gen

  # generate user selected locale.
  sed -i "s/# \(${LOCALE}\.UTF-8.*$\)/\1/g" /etc/locale.gen

  # For non-x86 platform, also enables non-UTF8 charset.
  if ! is_x86 && test x${LOCALE} = xzh_CN; then
    sed -i "s/# \(${LOCALE}\.GBK.*$\)/\1/g" /etc/locale.gen
    sed -i "s/# \(${LOCALE}\.GB18030.*$\)/\1/g" /etc/locale.gen
    sed -i "s/# \(${LOCALE}\.GB2312.*$\)/\1/g" /etc/locale.gen
  fi

  cat > /etc/default/locale << EOF
LANG=${LOCALE}.UTF-8
LANGUAGE=${LOCALE}
EOF

  # Re-generate localisation files.
  /usr/sbin/locale-gen

  echo "Check timezone ${DI_TIMEZONE}"
  if cat /usr/share/zoneinfo/zone.tab | grep -v '^#' | awk '{print $3}' | \
      grep -q "^${DI_TIMEZONE}$"; then
    msg "${DI_TIMEZONE} is available"
  else
    msg "Timezone is not available, Fall back to Asia/Shanghai"
    DI_TIMEZONE="Asia/Shanghai"
  fi

  msg "Set timezone to ${DI_TIMEZONE}"
  echo "${DI_TIMEZONE}" > /etc/timezone
  # Create /etc/localtime symbol links
  ln -nsf /usr/share/zoneinfo/${DI_TIMEZONE} /etc/localtime
  dpkg-reconfigure --frontend noninteractive tzdata

  # Set using local time or not
  local DI_IS_LOCAL_TIME
  DI_IS_LOCAL_TIME=$(installer_get "DI_IS_LOCAL_TIME")
  local timezone_use_local_time_regardless
  timezone_use_local_time_regardless=$(installer_get "timezone_use_local_time_regardless")
  if [ x"timezone_use_local_time_regardless" = xtrue -o x"${DI_IS_LOCAL_TIME}" = xtrue ]; then
    # Use RTC time, "timedatectl set-local-rtc 1"
    enable_local_rtc
  fi

  # Always enable NTP service.
  local readonly TIME_SERVICE=/etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service
  [ -f "${TIME_SERVICE}" ] || \
    ln -sv /lib/systemd/system/systemd-timesyncd.service "${TIME_SERVICE}"

  # Check locale
  if ls /usr/share/i18n/locales | grep -q "\<${LOCALE}\>"; then
    msg "${LOCALE} is available"
  else
    msg "${LOCALE} is not available, Fallback to en_US"
    unset LOCALE
  fi

  # Update grub based on current locale
  update-grub || true
}

if [ x$(installer_get "system_info_setup_after_reboot") != "xtrue" ]; then
  setup_locale_timezone
fi
